package com.goldenautumn.prefabricate.activity.mine

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.OrderMenuInfoVo
import com.goldenautumn.prefabricate.business.user.UserInfoUiState
import com.goldenautumn.prefabricate.business.user.UserViewModel
import com.goldenautumn.prefabricate.databinding.ActivityUserInfoBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.goldenautumn.prefabricate.utils.getUserInfo
import com.goldenautumn.prefabricate.utils.loadImgUrl
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.goldenautumn.prefabricate.widget.dialog.EditInfoDialog
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseActivity
import qncx.external.common.compat.isVisible
import qncx.external.common.compat.visible
import qncx.external.common.room.table.UserInfo

class UserInfoActivity : BaseActivity<ActivityUserInfoBinding>() {

    private val mUserModel by viewModels<UserViewModel>()

    override fun getLayoutId(): Int = R.layout.activity_user_info

    override suspend fun main() {
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        initToolbarNav(mBinding.toolbar)

        var userInfo = getUserInfo()
        updateInfo(userInfo)

        mBinding.nickEdit.setOnClickListener {
            var userInfo = getUserInfo()
            LogUtils.d(userInfo)
            EditInfoDialog(2){
                mUserModel.getUserInfo()
            }.onShow(supportFragmentManager.beginTransaction())
        }

        mBinding.phoneEdit.setOnClickListener {
            EditInfoDialog(1){
                mUserModel.getUserInfo()
            }.onShow(supportFragmentManager.beginTransaction())
        }

        mBinding.wechatEdit.setOnClickListener {
            var intent = Intent(ConstantsAPI.ACTION_REFRESH_WXAPP)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                registerReceiver(object : BroadcastReceiver() {
                    override fun onReceive(context: Context?, intent: Intent?) {
                        var api = WXAPIFactory.createWXAPI(this@UserInfoActivity, getString(R.string.wechatId), true)
                        api.registerApp(getString(R.string.wechatId))
                        var req: SendAuth.Req = SendAuth.Req()
                        req.scope = "snsapi_userinfo"
                        req.state = "wechat_sdk_photo"
                        var isSend = api.sendReq(req)
                        SPUtils.getInstance().put("IS_BIND",isSend)
                    }
                }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP), RECEIVER_EXPORTED)
            } else {
                registerReceiver(object : BroadcastReceiver() {
                    override fun onReceive(context: Context?, intent: Intent?) {
                        var api = WXAPIFactory.createWXAPI(this@UserInfoActivity, getString(R.string.wechatId), true)
                        api.registerApp(getString(R.string.wechatId))
                        var req: SendAuth.Req = SendAuth.Req()
                        req.scope = "snsapi_userinfo"
                        req.state = "wechat_sdk_photo"
                        var isSend = api.sendReq(req)
                        SPUtils.getInstance().put("IS_BIND",isSend)
                    }
                }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP))
            }
            sendBroadcast(intent)
        }

        while (isActive) {
            select {
                mUserModel.user.onReceive {
                    when(it) {
                        is UserInfoUiState.UserInfo -> {
                            updateInfo(it.userInfo)
                        }
                        is UserInfoUiState.UserUpdate -> {
                            if(it.isSuccess) mUserModel.getUserInfo()
                        }
                        else -> {}
                    }
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mUserModel.getUserInfo()
    }


    fun  updateInfo(userInfo: UserInfo){
        mBinding.nickName.text = userInfo.nickName
        mBinding.phone.text = if(userInfo.bindPhone == 1) userInfo.phone else getString(R.string.unbind)
        mBinding.wechat.text = if(userInfo.bindWechat != 1) getString(R.string.unbind) else getString(R.string.bind)
        mBinding.phoneEdit.isVisible(userInfo.bindPhone != 1)
        mBinding.wechatEdit.isVisible(userInfo.bindWechat != 1)
    }

}